<template>
  <div class="login">
    <div class="login-box">
      <img src="https://hellorfimg.zcool.cn/preview260/2471969811.jpg?x-image-process=image/format,webp" alt="" />
      <div class="login-form">
        <el-form :model="loginForm" :rules="rules" ref="formRef">
          <div class="login-form-title">
            <img src="../../assets/layouts/logo-text-2.png" style="width: 200px" alt="" />
          </div>
          <el-form-item prop="username">
            <el-input size="small" v-model="loginForm.username" placeholder="账号" :prefix-icon="User" />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              size="small"
              v-model="loginForm.password"
              type="password"
              :show-password="true"
              placeholder="密码"
              :prefix-icon="Lock"
            />
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button class="login-btn" size="small" type="primary" style="width: 100%" @click="login">
              <span>登录</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue"
import { User, Lock } from "@element-plus/icons-vue"
import { useUserStore } from "@/store/modules/user"
import { useRouter } from "vue-router"
import { type FormInstance } from "element-plus"

// 全局路由
const router = useRouter()

// 登录表单
const loginForm = ref({
  username: "admin",
  password: "123456"
})

// 用户仓库
const userStore = useUserStore()

// 点击登录
const login = async () => {
  // 表单校验
  formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      const res = await userStore.login(loginForm.value) //调用登录api，并保存token
      if (res) {
        router.push("/") // 跳转到首页
      }
    }
  })
}

// 表单校验
const rules = {
  username: [
    {
      required: true,
      message: "请输入账号",
      trigger: "blur"
    }
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur"
    }
  ]
}

// 表单对象
const formRef = ref<FormInstance>()
</script>

<style lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  // background: #476dbe;
  background-color: #333;
  border-radius: 8px;

  .login-box {
    width: 1000px;
    height: 474.38px;
    border-radius: 8px;
    display: flex;
    img {
      width: 60%;
      height: auto;
      border-radius: 8px 0 0 8px;
    }
  }
}
.title {
  margin: 0px auto 10px auto;
  text-align: left;
  color: #707070;
}

.login-form {
  background: #ffffff;
  width: 40%;
  border-radius: 0px 8px 8px 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-form {
    width: 214px;
    height: 307px;
  }
  .el-form-item {
    margin-bottom: 30px;
  }
  .el-form-item.is-error .el-input__inner {
    border: 0 !important;
    background: #fff !important;
  }
  .input-icon {
    height: 32px;
    width: 18px;
    margin-left: -2px;
  }
  .el-input__inner {
    border: 0;
    border-bottom: 1px solid #e9e9e8;
    border-radius: 0;
    font-size: 12px;
    font-weight: 400;
    color: #333333;
    height: 32px;
    line-height: 32px;
  }
  .el-input__prefix {
    left: 0;
  }
  .el-input--prefix .el-input__inner {
    padding-left: 10px;
  }
  .el-input__inner::placeholder {
    color: #aeb5c4;
  }
  .el-form-item--medium .el-form-item__content {
    line-height: 32px;
  }
  .el-input--medium .el-input__icon {
    line-height: 32px;
  }
}

.login-btn {
  border-radius: 17px;
  padding: 15px 20px !important;
  margin-top: 10px;
  font-weight: 500;
  font-size: 14px;
  border: 0;
  font-weight: 500;
  color: #fff;
  // background: #09a57a;
  background-color: #ffc200;
  &:hover,
  &:focus {
    // background: #09a57a;
    background-color: #ffc200;
    color: #ffffff;
  }
  overflow: hidden;
  transition: all 0.5s;
  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: -40%;
    width: 20%;
    height: 100%;
    background: linear-gradient(90deg, transparent, #fff, transparent);
    /* 倾斜 */
    transform: skewX(-45deg);
  }
  &:hover {
    background-color: #ff7b0f;
    &::before {
      left: 120%;
      transition: all 0.5s;
    }
  }
}
.login-form-title {
  height: 36px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 40px;
  .title-label {
    font-weight: 500;
    font-size: 20px;
    color: #333333;
    margin-left: 10px;
  }
}
</style>
